﻿using System;
using System.ComponentModel;
using System.Deployment.Application;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using AutoTAU.Properties;

namespace AutoTAU
{
    public class NotificationIcon : Form
    {
        private readonly ContextMenuStrip contextMenuStrip;
        private readonly ToolStripMenuItem toolStripMenuItemAbout;
        private readonly ToolStripMenuItem toolStripMenuItemAuthenticate;
        private readonly ToolStripMenuItem toolStripMenuItemHelp;
        private readonly ToolStripMenuItem toolStripMenuItemQuit;
        private readonly ToolStripMenuItem toolStripMenuItemSettings;
        private readonly ToolStripMenuItem toolStripMenuItemUpdate;
        public NotifyIcon sysTrayIcon;

        public NotificationIcon()
        {
            // Create and configure toolstrips
            toolStripMenuItemSettings = new ToolStripMenuItem
                                            {
                                                Name = "toolStripMenuItemSettings",
                                                Size = new Size(152, 22),
                                                Text = "הגדרות"
                                            };
            toolStripMenuItemSettings.Click += toolStripMenuItemSettings_Click;
            toolStripMenuItemAuthenticate = new ToolStripMenuItem
                                                {
                                                    Name = "toolStripMenuItemSettings",
                                                    Size = new Size(152, 22),
                                                    Text = "כנס עכשיו"
                                                };
            toolStripMenuItemAuthenticate.Click += toolStripMenuItemAuthenticate_Click;
            toolStripMenuItemUpdate = new ToolStripMenuItem
                                          {
                                              Name = "toolStripMenuItemUpdate",
                                              Size = new Size(152, 22),
                                              Text = "בדוק עדכונים ידנית"
                                          };
            toolStripMenuItemUpdate.Click += toolStripMenuItemUpdate_Click;
            toolStripMenuItemHelp = new ToolStripMenuItem
                                        {
                                            Name = "toolStripMenuItemHelp",
                                            Size = new Size(152, 22),
                                            Text = "עזרה"
                                        };
            toolStripMenuItemHelp.Click += toolStripMenuItemHelp_Click;
            toolStripMenuItemAbout = new ToolStripMenuItem
                                         {
                                             Name = "toolStripMenuItemAbout",
                                             Size = new Size(152, 22),
                                             Text = "אודות"
                                         };
            toolStripMenuItemAbout.Click += toolStripMenuItemAbout_Click;
            toolStripMenuItemQuit = new ToolStripMenuItem
                                        {
                                            Name = "toolStripMenuItemQuit",
                                            Size = new Size(152, 22),
                                            Text = "צא"
                                        };
            toolStripMenuItemQuit.Click += toolStripMenuItemQuit_Click;

            // Create and configure context menu strip
            contextMenuStrip = new ContextMenuStrip
                                   {
                                       Name = "contextMenuStrip",
                                       Size = new Size(153, 92),
                                       RightToLeft = RightToLeft.Yes
                                   };
            contextMenuStrip.Items.AddRange(new ToolStripItem[]
                                                {
                                                    toolStripMenuItemSettings,
                                                    toolStripMenuItemAuthenticate,
                                                    toolStripMenuItemUpdate,
                                                    toolStripMenuItemHelp,
                                                    toolStripMenuItemAbout,
                                                    toolStripMenuItemQuit
                                                });

            // Create and configure notification icon
            sysTrayIcon = new NotifyIcon
                              {
                                  ContextMenuStrip = contextMenuStrip,
                                  Icon = Resources.IconBlue16,
                                  Text = "AutoTAU",
                                  Visible = true
                              };
            sysTrayIcon.DoubleClick += sysTrayIcon_DoubleClick;
        }

        private static void sysTrayIcon_DoubleClick(object sender, EventArgs e)
        {
            WifiAuthManager.Register();
            AuthenticateNow();
        }

        private static void toolStripMenuItemSettings_Click(object sender, EventArgs e)
        {
            StartSettingsForm();
        }

        private static void toolStripMenuItemAuthenticate_Click(object sender, EventArgs e)
        {
            AuthenticateNow();
        }

        private static void toolStripMenuItemUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                if (ApplicationDeployment.CurrentDeployment.Update())
                {
                    DialogResult dlgRes = MessageBox.Show("הותקן עדכון לתוכנה. להפעיל מחדש?", "עדכון",
                                                          MessageBoxButtons.YesNo,
                                                          MessageBoxIcon.Question, MessageBoxDefaultButton.Button1,
                                                          MessageBoxOptions.RtlReading);
                    if (dlgRes == DialogResult.Yes)
                    {
                        Application.Restart();
                    }
                }
                else
                {
                    MessageBox.Show("אין חדש תחת השמש", "עדכון", MessageBoxButtons.OK, MessageBoxIcon.Information,
                                    MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "שגיאת עדכון", MessageBoxButtons.OK,
                                MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading);
            }
        }

        private static void toolStripMenuItemHelp_Click(object sender, EventArgs e)
        {
            OpenHelpPage();
        }

        /// <summary>
        /// Open the help webpage
        /// </summary>
        private static void OpenHelpPage()
        {
            try
            {
                Process.Start("http://sites.google.com/site/autotauproject/instructions");
            }

            catch (Win32Exception)
            {
                // This happens when there's no default browser. Ignore.
            }
        }

        private static void toolStripMenuItemAbout_Click(object sender, EventArgs e)
        {
            About();
        }

        private static void toolStripMenuItemQuit_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private static void StartSettingsForm()
        {
            (new SettingsForm()).ShowDialog();
        }

        private static void AuthenticateNow()
        {
            Action authAction = WifiAuthManager.Authenticate;
            authAction.BeginInvoke(authAction.EndInvoke, null);
        }

        private static void About()
        {
            (new AboutBox()).ShowDialog();
        }
    }
}